package com.snail.sort;

public class MySort {

    /**
     * 冒泡排序
     *
     * @param arr
     * @return
     */
    public Object sort01(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            Boolean flag = true;
            for (int j = 0; j < arr.length - i; j++) {

                if (arr[j] > arr[j + 1]) {
                    int tem = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tem;
                    flag = false;
                }
            }
            if (flag) {
                return arr;
            }
        }
        return arr;
    }

    /**
     * 选择排序
     */
    public Object sort02(int[] arr){
        for(int i=0; i<arr.length-1; i++){
            int index = i;
            for(int j=index+1; j<arr.length; j++){
                if(arr[i]>arr[j]){
                    index = j;
                }
            }
            if(index != i){
                int tem = arr[i];
                arr[i] = arr[index];
                arr[index] = tem;
            }
        }
        return arr;
    }

    /**
     * 插入排序
     */
    public Object sort03(int[] arr){
        int j;
        for (int i=1; i<arr.length; i++){
            j = i;
            int tem = arr[i];
            while (j>0 && tem<arr[j-1]){
                arr[j] = arr[j-1];
                j--;
            }
            arr[j] = tem;
        }
        return arr;
    }
}

